package com.example.demo.modules.wiki.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.R;
import com.example.demo.modules.wiki.entity.Story;
import com.example.demo.modules.wiki.service.StoryService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

import static com.example.demo.common.util.Utils.isNil;

@RestController
@RequestMapping("/story")
public class StoryController {

    @Autowired
    private StoryService storyService;


    @GetMapping("/listAll")
    public R listAll() {
        return R.ok().put("result",storyService.list(null));
    }

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",dataType="String"),
            @ApiImplicitParam(name="pageNum",dataType="String")
    })
    public R page(
            Integer pageSize,Integer pageNum) {
        if (isNil(pageSize)) {
            pageSize = 10;
        }
        if (isNil(pageNum)) {
            pageNum = 0;
        }
        QueryWrapper<Story> queryWrapper = new QueryWrapper<>();
        return R.ok().put("result",storyService.page(new Page<>(pageNum,pageSize), queryWrapper));
    }
    @PostMapping("/add")
    public R add(@RequestBody Story story) {
        return R.ok().put("result",storyService.save(story));
    }
    @PostMapping("/update")
    public R update(@RequestBody  Story story, HashMap<String, Object> updateOptions) {
        QueryWrapper<Story> queryWrapper=new QueryWrapper<>();
        if(!isNil(story.getId())){
            queryWrapper.eq("id",story.getId());
        }else{
            return R.error("id不能为空");
        }
        return R.ok().put("result",storyService.update(story,queryWrapper));
    }
}
